#! /bin/bash

#check to see if there is a tossfile in this directory
if !(test -d "tossfile" || test -c "tossfile.py")
    # no tossfile in this directory
    then exit
fi
#check to see if any of the tossfiles are newer than the completions file
# TODO: see if we can use md5s instead of timestamps
newer=0

for line in $(ls tossfile/*.py) 
  do if test .toss_completions -ot $line 
    then newer=1 
  fi
done
if test .toss_completions -ot tossfile.py
  then newer=1
fi

if test $newer -eq 1
  then python -c "
import time
import sys
import os
import pytoss
from pytoss._tasks import TaskManager

pytoss.Tossfile( os.path.abspath( os.getcwd() ) )
tasks = TaskManager.get_instance().get_all_tasks()

completions = ''
for task in tasks:
  completions += task.name + '\n'
f = open('.toss_completions', 'w')
f.write(completions)
"
fi

if test -r ".toss_completions"
  then cat .toss_completions
fi
